﻿using System.Net;
using System.Net.Sockets;
using System.Text;

namespace SocketSender
{
    internal class TCPClientSender
    {
        internal TcpClient TcpClient { get; set; }

        public TCPClientSender(IPAddress serverIPAddress, int serverPort)
        {
            var endPoint = new IPEndPoint(serverIPAddress, serverPort);

            TcpClient = new TcpClient();

            TcpClient.Connect(endPoint);
        }

        public void SendMessageToServer(string serverMessage)
        {
            NetworkStream clientStream = TcpClient.GetStream();

            byte[] bufferedMessage = GetBufferedMessage(serverMessage);

            clientStream.Write(bufferedMessage, 0, bufferedMessage.Length);

            clientStream.Flush();
        }

        private static byte[] GetBufferedMessage(string serverMessage)
        {
            return Encoding.UTF32.GetBytes(serverMessage);
        }
    }
}
